package com.ruoyi.edu.service.impl;

import java.util.List;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.edu.domain.PaperStudent;
import com.ruoyi.edu.mapper.PaperStudentMapper;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.mapper.PaperMapper;
import com.ruoyi.edu.domain.Paper;
import com.ruoyi.edu.service.IPaperService;

/**
 * 论文Service业务层处理
 * 
 * @author sanda
 * @date 2025-03-10
 */
@Service
public class PaperServiceImpl implements IPaperService
{
    @Override

    public void sendNotification(Long[] paperIds) {

        StringBuilder sb = new StringBuilder("【通知】");

        sb.append("以下论文已提交：\n");

        for (Long paperId: paperIds) {

            Paper paper = selectPaperByPaperId(paperId);

            sb.append(paper.getPaperName());

            sb.append("\n");

        }

        sb.append("请及时审核！");

        sendDingTalkMsg(sb.toString());

    }

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PaperStudentMapper paperStudentMapper;

//    public static void main(String[] args) {
//
//        String content = "【通知】欢迎各位同学参加本周的课程";
//
//        sendDingTalkMsg(content);
//
//    }

    private void sendDingTalkMsg(String content) {

        DingTalkClient client = new DefaultDingTalkClient("\n" +
                "https://oapi.dingtalk.com/robot/send?access_token=11e7f8d6b87359af76f9b2bd210513f0c813a78ab15559188dd6ae47e254c017");

        OapiRobotSendRequest request = new OapiRobotSendRequest();

        request.setMsgtype("text");

        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();

        text.setContent(content);

        request.setText(text);

        try {

            OapiRobotSendResponse response = client.execute(request);

        } catch (ApiException e) {

            e.printStackTrace();

            throw new RuntimeException("发送钉钉消息失败");

        }

    }


    /**
     * 查询论文
     * 
     * @param paperId 论文主键
     * @return 论文
     */




    @Override
    public Paper selectPaperByPaperId(Long paperId)
    {
        return paperMapper.selectPaperByPaperId(paperId);
    }

    /**
     * 查询论文列表
     * 
     * @param paper 论文
     * @return 论文
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Paper> selectPaperList(Paper paper)
    {
        return paperMapper.selectPaperList(paper);
    }
    @Override

    public List<Paper> selectAll(Paper paper) {

        return paperMapper.selectPaperList(paper);

    }

    /**
     * 新增论文
     * 
     * @param paper 论文
     * @return 结果
     */
    @Override
    public int insertPaper(Paper paper)
    {
        int rows = paperMapper.insertPaper(paper);
        for (Long studentId: paper.getStudentIds()) {
            PaperStudent ps = new PaperStudent();
            ps.setPaperId(paper.getPaperId());
            ps.setStudentId(studentId);
            paperStudentMapper.insert(ps);
        }
        return rows;

        //return paperMapper.insertPaper(paper);
    }

    /**
     * 修改论文
     * 
     * @param paper 论文
     * @return 结果
     */
    @Override
    public int updatePaper(Paper paper)
    {
        paperStudentMapper.delete(paper.getPaperId());
        insertPaperStudent(paper);
        return paperMapper.updatePaper(paper);
    }

    private void insertPaperStudent(Paper paper) {
        for (Long studentId : paper.getStudentIds()) {
            PaperStudent ps = new PaperStudent();
            ps.setPaperId(paper.getPaperId());
            ps.setStudentId(studentId);
            paperStudentMapper.insert(ps);
        }
    }


    /**
     * 批量删除论文
     * 
     * @param paperIds 需要删除的论文主键
     * @return 结果
     */
    @Override
    public int deletePaperByPaperIds(Long[] paperIds)
    {
        return paperMapper.deletePaperByPaperIds(paperIds);
    }

    /**
     * 删除论文信息
     * 
     * @param paperId 论文主键
     * @return 结果
     */
    @Override
    public int deletePaperByPaperId(Long paperId)
    {
        return paperMapper.deletePaperByPaperId(paperId);
    }
}
